package com.wrupple.muba.base.client.module.services.logic;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.ResettableEventBus;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.place.shared.PlaceHistoryMapper;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.wrupple.muba.base.client.module.MubaDesktopProcessManager;
import com.wrupple.muba.base.client.module.services.presentation.impl.MubaDesktopManager;
import com.wrupple.muba.common.client.activity.AbstractDesktopModuleController;
import com.wrupple.muba.common.shared.ServiceBus;
import com.wrupple.muba.common.shared.State.DesktopManager;
import com.wrupple.muba.common.shared.State.ProcessManager;
import com.wrupple.muba.desktop.client.module.DesktopActivityMapper;
import com.wrupple.muba.desktop.client.module.DesktopModuleController;
import com.wrupple.muba.widget.client.activity.ActivityPresenter;

@Singleton
public class MubaDesktopModuleController extends
		AbstractDesktopModuleController implements DesktopModuleController,
		Provider<DesktopModuleController>{

	protected DesktopManager dm;
	protected EventBus eventbus;
	protected ServiceBus serviceBus;
	private PlaceController placeController;
	private MubaDesktopProcessManager webapp;
	private ActivityPresenter activityPresenter;
	private PlaceHistoryHandler paceHistoryHandler;

	@Inject
	public MubaDesktopModuleController(EventBus bus, ServiceBus s,
			DesktopManager dm, MubaDesktopProcessManager webapp,
			ActivityPresenter activityPresenter,
			DesktopActivityMapper activityMapper,
			PlaceHistoryMapper historyMapper) {
		super();
		this.webapp = webapp;
		this.eventbus = new ResettableEventBus(bus);
		this.placeController = new PlaceController(eventbus);
		this.dm = new MubaDesktopManager(eventbus);
		activityPresenter.setEventBus(this.eventbus);
		this.activityPresenter = activityPresenter;
		this.serviceBus = s;
		final ActivityManager activityManager = new ActivityManager(
				activityMapper, eventbus);
		paceHistoryHandler = new PlaceHistoryHandler(historyMapper);
		paceHistoryHandler.register(placeController, eventbus,
				dm.getDefaultPlace());
		activityManager.setDisplay(activityPresenter.getOutputFeature());

	}


	@Override
	public void initialize() {
		RootLayoutPanel.get().add(activityPresenter);
	}

	@Override
	public Map<String, Activity> getActivities() {
		//String homeActivityId = dm.getDefaultPlace().getActivity();
		Map<String, Activity> regreso = new HashMap<String, Activity>();
		return regreso;
	}


	@Override
	public EventBus getEventBus() {
		return this.eventbus;
	}

	@Override
	public ServiceBus getServiceBus() {
		return this.serviceBus;
	}

	@Override
	public DesktopManager getDesktopManager() {
		return this.dm;
	}

	@Override
	public DesktopModuleController get() {
		return staticget();
	}

	@Override
	public PlaceController getPlaceController() {
		return placeController;
	}

	@Override
	public ProcessManager getProcessManager() {
		return webapp;
	}

	@Override
	public ActivityPresenter getActivityPresenter() {
		return activityPresenter;
	}

	@Override
	public PlaceHistoryHandler getPlaceHistoryHandler() {
		return paceHistoryHandler;
	}


	@Override
	public void registerServices(ServiceBus bus) {
		// TODO Auto-generated method stub
		
	}

}
